from robot.api import logger
from robotlibcore import keyword

class OecModel(object):

    @keyword('Create Oec Power')
    def create_oec_power(self, powerType, customerId, charge_mode, control_mode):
        
        power_template_s = "INSERT INTO oec_consumable_empower_info (`customer_id`, `customer_entity_id`, `service_code`, `service_value`, `service_type`, `source_type`, `source_value`, `original_source_value`, `charge_mode`, `control_mode`, `effective_start_date`, `effective_end_date`, `status`, `create_time`, `last_modified`) VALUES "
    
        service_code = "PTS_JG_SWXD"
        service_value = "6000"
        service_type = "01"

        srouce_value = customerId

        effective_start_date, effective_end_date = self._get_power_effective_date(powerType, charge_mode)

        
        power_value_s = "('{0}', null, '{1}', {2}, '{3}', 'order', '{4}', null, {5}, {6}, {7}, {8}, '1', now(), now())".format(customerId, service_code, service_value, service_type, srouce_value, charge_mode, control_mode, effective_start_date, effective_end_date)
        value = power_template_s + power_value_s
        logger.info('[OEC] 生成的授权sql: {}'.format(value))

        return value

    def _get_power_effective_date(self, powerType, chargeMode):

        if powerType == "powering":
            effective_start_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval -1 month), '%Y-%m-%d')"

            if int(chargeMode) == 2:
                effective_end_date = "null"
            else:
                effective_end_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval +1 month), '%Y-%m-%d')"
        
        if powerType == "not-power":
            effective_start_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval +1 month), '%Y-%m-%d')"
            if int(chargeMode) == 2:
                effective_end_date = "null"
            else:
                effective_end_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval +2 month), '%Y-%m-%d')"
        
        if powerType == "powered":
            effective_start_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval -2 month), '%Y-%m-%d')"
            effective_end_date = "DATE_FORMAT( date_add(curdate()-day(curdate())+1,interval -1 month), '%Y-%m-%d')"


        return effective_start_date, effective_end_date
    
    @keyword('Get Oec Power Key')
    def get_oec_power_key(self, customerId, serviceCode, serviceType):

        return "{}:{}:{}".format(customerId, serviceCode, serviceType)